﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.EndItem.CUiEITFindEndItemRootCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.EndItem;

public sealed class CUiEITFindEndItemRootCmd : TxButtonCommand
{
  private CApEITFindEndItemRootEnabler m_enabler;

  public CUiEITFindEndItemRootCmd() => this.m_enabler = new CApEITFindEndItemRootEnabler();

  public virtual string Category => EndItemStringTable.CMD_END_ITEM_CATEGORY;

  public virtual string Name => EndItemStringTable.CMD_END_ITEM_FIND_ROOT_NAME;

  public virtual string Tooltip => EndItemStringTable.CMD_END_ITEM_FIND_ROOT_TOOLTIP;

  public virtual string Bitmap => "EndItem.FindEndItemIcon.bmp";

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("FindEndItemRoot");

  public virtual string Description => EndItemStringTable.CMD_END_ITEM_FIND_ROOT_DESCRIPTION;

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_enabler;

  public virtual void Execute(object cmdParams)
  {
    ITxEndItemBase itemRepresentation = (((Collection<ITxObject>) CApEITEndItemApp.GetSelectedEndItems())[0] as ITxEndItemProvider).EndItemRepresentation;
    if (itemRepresentation == null || itemRepresentation.EndItemRoot == null)
      return;
    ITxEndItemRoot visibleEndItemRoot = this.GetVisibleEndItemRoot(itemRepresentation.EndItemRoot);
    if (visibleEndItemRoot == null)
      return;
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(((ITxEndItemBase) visibleEndItemRoot).EngineeringRepresentation);
    TxApplication.ActiveSelection.Clear();
    TxApplication.ActiveSelection.SetItems(txObjectList);
    TxApplication.RefreshDisplay();
  }

  private ITxEndItemRoot GetVisibleEndItemRoot(ITxEndItemRoot descendant)
  {
    if (((ITxEndItemBase) descendant).EndItemRoot != null)
    {
      ITxEndItemRoot visibleEndItemRoot = this.GetVisibleEndItemRoot(((ITxEndItemBase) descendant).EndItemRoot);
      if (!visibleEndItemRoot.IsDescendantsExposed)
        return visibleEndItemRoot;
    }
    return descendant;
  }
}
